Transform Weather Sensor Data from Waggle to an Xarray Dataset + Plot with ACT#
Imports#
import sage_data_client
from bokeh.models.formatters import DatetimeTickFormatter
import hvplot.pandas
import hvplot.xarray
import holoviews as hv
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import xarray as xr
import matplotlib.pyplot as plt
from metpy.plots import USCOUNTIES
import act
import numpy as np
import pandas as pd
import warnings
from bokeh.models import DatetimeTickFormatter
def apply_formatter(plot, element):
plot.handles['xaxis'].formatter = DatetimeTickFormatter(hours='%m/%d/%Y \n %H:%M',
minutes='%m/%d/%Y \n %H:%M',
hourmin='%m/%d/%Y \n %H:%M',
days='%m/%d/%Y \n %H:%M',
months='%m/%d/%Y \n %H:%M')
xr.set_options(keep_attrs=True)
warnings.filterwarnings("ignore")
hv.extension("bokeh")
Query for the available Data#
wxt_df = sage_data_client.query(
start="-3h",
filter={
"sensor": "vaisala-wxt536"
}
)
Configure Helper Functions and Renaming Conventions#
The renaming is required due to . notations being problematic when working with both Pandas and Xarray data structures.
variable_rename_dict = {'wxt.env.humidity':'air_humidity',
'wxt.env.pressure':'air_pressure',
'wxt.env.temp':'air_temperature',
'wxt.heater.temp':'heater_temperature',
'wxt.heater.volt':'heater_voltage',
'wxt.rain.accumulation':'rain_accumulation',
'wxt.wind.direction':'wind_direction',
'wxt.wind.speed':'wind_speed',
'sys.gps.lat':'latitude',
'sys.gps.lon':'longitude',
}
def generate_data_array(df, variable, rename_variable_dict=variable_rename_dict):
new_variable_name = rename_variable_dict[variable]
df_variable= df.loc[df.name == variable]
ds = df_variable.to_xarray().rename({'value':new_variable_name,
'timestamp':'time',
'meta.vsn':'node'})
ds[new_variable_name].attrs['units'] = df_variable['meta.units'].values[0]
ds['time'] = pd.to_datetime(ds.time)
ds.attrs['datastream'] = ds.node.values[0]
return ds[[new_variable_name]]
def generate_dataset(df, variables, rename_variable_dict=variable_rename_dict):
reindexed = df.set_index(['meta.vsn', 'timestamp'])
return xr.merge([generate_data_array(reindexed, variable) for variable in variables])
Transform the Data to Xarray#
wxt_variables = wxt_df.name.unique()
wxt_variables
array(['wxt.env.humidity', 'wxt.env.pressure', 'wxt.env.temp',
'wxt.heater.temp', 'wxt.heater.volt', 'wxt.rain.accumulation',
'wxt.wind.direction', 'wxt.wind.speed'], dtype=object)
wxt_ds = generate_dataset(wxt_df, wxt_variables).squeeze()
wxt_ds
<xarray.Dataset> Size: 82MB
Dimensions: (node: 4, time: 311702)
Coordinates:
* node (node) object 32B 'W01B' 'W067' 'W08D' 'W099'
* time (time) datetime64[ns] 2MB 2024-04-09T11:45:16.5387165...
Data variables:
air_humidity (node, time) float64 10MB nan 67.6 nan ... nan nan nan
air_pressure (node, time) float64 10MB nan 997.8 nan ... nan 988.6
air_temperature (node, time) float64 10MB nan 9.7 nan ... 15.7 nan 15.7
heater_temperature (node, time) float64 10MB nan 13.4 nan ... 19.6 nan 19.6
heater_voltage (node, time) float64 10MB nan 11.4 nan ... 23.9 nan 23.8
rain_accumulation (node, time) float64 10MB nan 342.1 nan ... nan 311.0
wind_direction (node, time) float64 10MB nan 220.0 nan ... nan 261.0
wind_speed (node, time) float64 10MB nan 1.6 nan ... 2.3 nan 2.3
Attributes:
datastream: W01BResample the data to minute requency#
minute_ds = wxt_ds.resample(time='1T').mean()
Visualize using hvplot#
meteogram_variables = ['air_temperature', 'air_humidity', 'wind_speed', 'wind_direction']
plots = []
for variable in meteogram_variables:
plots.append(wxt_ds[variable].hvplot.line(label='10 Hz Data') *
minute_ds[variable].hvplot.line(label='1 Minute Data'))
hv.Layout(plots).cols(2)